package com.nondo.dean.saprfc.controller;

import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.View;
import com.nondo.dean.saprfc.common.Util;
import com.nondo.dean.saprfc.domain.ProductBomStructure;
import com.nondo.dean.saprfc.service.CrmProductBomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;

/**
 * @author xiaofan
 * @Description:
 * @throws
 * @date 2018-03-3015:30
 */
@RestController
@RequestMapping("/{v}/saprfc/product/bom")
public class CrmProductBomController {

    @Autowired
    private CrmProductBomService crmProductBomService;

    @Autowired
    RestTemplate restTemplate;


    /**
     * 调用servic层获取bom结构方法
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    @HystrixCommand(fallbackMethod = "ErrorFallback")
    public View<String> getSapBom(){
        View<String> view = new View<>(20000,"同步成功");
        Util.reInitMessageConverter(restTemplate);
        ArrayList<ProductBomStructure> productBomStructuresList=crmProductBomService.getBomHeadDate("");
        String resultBom= JSON.toJSONString(productBomStructuresList);
        int bomCount = restTemplate.postForEntity("http://dean-product/product/v1/inner/product/bom/sapSynBom", resultBom, Integer.class).getBody();
        view.setData("更新'"+bomCount+"'条BOM数据");
        return view;
    }

}
